<template>
	<view class="mainpadding">
		<view class="sishi xiaobai fonweight margin_top6">输入手机号</view>
		<view class="margin_top6 xiahuaxian">
			<input type="number" class="ziti" placeholder="请输入您的手机号码" v-model="mobile">
		</view>
		<view class="margin_top xiahuaxian flexbetween">
			<input type="number" class="ziti" placeholder="请输入手机验证码"  v-model="yzm_code">
			<view class="xiaohong ershiba textcenter" style="width: 240rpx;" @click="sendyzm"
				v-show="codetext == 1">获取验证码</view>
			<view class="xiaohong ershiba textcenter" style="width: 240rpx;" v-show="codetext == 2">
				{{second}}s后重新获取</view>
			<view class="xiaohong ershiba textcenter" style="width: 240rpx;" @click="sendyzm"
				v-show="codetext == 3">重新发送</view>
		</view>
		<view class="margin_top6 xiahuaxian">
			<input type="number" class="ziti" placeholder="请输入新的密码" v-model="newpassword">
		</view>
		<view class="margin_top6 xiahuaxian">
			<input type="number" class="ziti" placeholder="请再次输入新的密码" v-model="newpasswordt">
		</view>
		<view class="bigbtn margin_top6" @click="signIn()" v-if="!liandiantype">完成</view>
		<view class="bigbtn margin_top6" v-if="liandiantype">完成</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				codetext: 1,
				second: 59,
				mobile: '',
				yzm_code: '',
				newpassword: "",
				newpasswordt: "",
				liandiantype: 0
			}
		},
		methods: {
			
			//验证手机号
			isMobile(value) {
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
				return a;
			},
			//发送验证码
			sendyzm() {
				let _this = this;
				if (_this.mobile == '') {
					httpRequest.toast("请输入手机号码!")
					return;
				}
				if (!_this.isMobile(_this.mobile)) {
					httpRequest.toast("手机号码格式错误!")
					return;
				}
				httpRequest.request('/api/sms/send', 'POST', {
					mobile: this.mobile,
					event: 'resetpwd',
				}, false, true, false).then(res => {
					if (res.code == 1) {
						httpRequest.toast('发送成功')
						_this.codetext = 2;
						_this.second = 59;
						let timer = setInterval(function() {
							if (_this.second > 1) {
								_this.second--;
							} else {
								clearInterval(timer);
								_this.codetext = 3;
							}
						}, 1000)
					}else{
						httpRequest.toast(res.msg)
					}
				})
			},
			signIn() {
				if (this.mobile == "") {
					httpRequest.toast("请输入手机号");
					return false;
				}
				if (!this.isMobile(this.mobile)) {
					httpRequest.toast("手机号码格式错误!")
					return;
				}
				if (this.yzm_code == "") {
					httpRequest.toast("请输入验证码");
					return false;
				}
				if (this.newpassword == "") {
					httpRequest.toast("请输入密码")
					return false
				}
				if (this.newpasswordt == "") {
					httpRequest.toast("请再次输入密码")
					return false
				}
				if (this.newpasswordt != this.newpassword) {
					httpRequest.toast("两次密码输入不一致，请重试")
					return false
				}
				this.liandiantype = 1
				uni.showLoading({
					mask: true,
					title: "请稍候"
				})
				httpRequest.request('/api/user/resetpwd', 'GET', {
					mobile: this.mobile,
					captcha: this.yzm_code,
					newpassword: this.newpassword
				}).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg);
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					} else {
						this.liandiantype = 0
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.ziti {
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #999999;
	}
</style>